package com.ljb.flabbybird;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * SurfaceView基类
 * Created by ljb on 2017/7/7.
 */

public class BaseSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    /**
     * SurfaceView控制器
     */
    private SurfaceHolder mHolder;
    /**
     * canvas
     */
    private Canvas mCanvas;
    /**
     * 线程
     */
    private Thread t;
    /**
     * SurfaceView是否在运行
     */
    private boolean isRunning;
    /**
     * 页面刷新时间
     */
    private long intervalTime = 50;
    /**
     * 宽
     */
    protected int mWidth;
    /**
     * 高
     */
    protected int mHeight;
    /**
     * surfaceView控件的矩形框
     */
    protected RectF mSurfaceViewRect = new RectF();

    public BaseSurfaceView(Context context) {
        this(context, null);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (oldw == oldh && w > h) {//获取控件的大小
            mWidth = w;
            mHeight = h;
            mSurfaceViewRect.set(0, 0, mWidth, mHeight);
        }
    }

    /**
     * 初始化
     */
    private void init() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        setZOrderOnTop(true);//设置控件位于视图层顶部
        mHolder.setFormat(PixelFormat.TRANSLUCENT);//设置背景透明
        setFocusable(true);//设置获取焦点
        setFocusableInTouchMode(true);
        setKeepScreenOn(true);//设置屏幕常亮
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 开启线程
        isRunning = true;
        t = new Thread(this);
        t.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isRunning = false;
    }

    @Override
    public void run() {
        while (isRunning) {
            long start = System.currentTimeMillis();
            draw();
            long end = System.currentTimeMillis();
            if (end - start < intervalTime) {
                SystemClock.sleep(intervalTime - (end - start));
            }
        }
    }

    private void draw() {
        try {
            mCanvas = mHolder.lockCanvas();//获取canvas
            if (mCanvas != null) {
                drawSomething(mCanvas);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mCanvas != null) {
                mHolder.unlockCanvasAndPost(mCanvas);//释放canvas
            }
        }
    }

    /**
     * 绘制画面
     *
     * @param canvas
     */
    protected void drawSomething(Canvas canvas) {

    }
}
